package cn.fxyz.web.controller;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.fxyz.cart.Cart;
import cn.fxyz.cart.CartItem;
import cn.fxyz.entity.Dinnertable;
import cn.fxyz.entity.Orderdetail;
import cn.fxyz.entity.Orders;
import cn.fxyz.entity.User;
import cn.fxyz.page.PageBean;
import cn.fxyz.service.OrderService;
import cn.fxyz.util.PaymentUtil;
import java.io.IOException;
@Controller
@RequestMapping("/Orders")//order
public class OrderController {
	@Resource
	private OrderService orderService;
	@RequestMapping("findAll")
	//商家操作
	/* 商家查找所有订单*/
	public String findAll(@RequestParam(value="pageNum",required=false)Integer pageNum, Model model) {
		if(pageNum==null){
			pageNum=1;
		}
		PageBean<Orders> page = orderService.findAll(pageNum);
		if(page!=null){
			model.addAttribute("page", page);
			return "backstage/detail/orderList";
		}else{
			return "backstage/detail/error";
		}
	}
	/* 商家查找订单详情*/
	@RequestMapping("toOrderDetail")
	public String toOrderDetail(@RequestParam("orderId")int orderId,Model model) {
		Orders o = orderService.findByOrderId(orderId);
		if(o!=null){
			model.addAttribute("order", o);
			return "backstage/detail/orderDetail";
		}else{
			return "backstage/detail/error";
		}
	}
	/* 商家对订单进行结账 */
	@RequestMapping("settle")
	@ResponseBody
	public String settle(@RequestParam("orderId")int orderId,Model model) {
		int i= orderService.modifyStatusByOrderId(orderId);
		if(i==1){
			return "ok";
		}else{
			return null;
		}
	}
	/*用户下单*/
	@RequestMapping("placeAnOrder")
	public String placeAnOrder(Orders order,
			HttpServletRequest request,HttpServletResponse response,Model model) {
		HttpSession session=request.getSession();
		//获取购物车数据
		Cart cart = (Cart) session.getAttribute("cart");
		System.out.println(cart);
		System.out.println(session.getAttribute("cart"));
		//判断用户是否登陆
		if(session.getAttribute("user")==null) {
			return "login/login";
		}
		else if(session.getAttribute("user")!=null && cart != null){
			/////////////////////////////////////////
			//这里之后再修改，传入餐桌id即可
			Dinnertable dinnertable = new Dinnertable();
			dinnertable.setId(11);
			/////////////////////////////////////////
			//设置订单数据
			order.setTotalprice((float) cart.getTotal());
			order.setOrderdate(new Date());
			order.setOrderstatus(1);
			order.setUser((User) session.getAttribute("user"));
			order.setDinnertable(dinnertable);
			//设置orderDetail数据
			for (CartItem cartItem : cart.getCartItems()) {
				Orderdetail orderdetail = new Orderdetail();
				orderdetail.setFood(cartItem.getFood());
				orderdetail.setFoodcount(cartItem.getFoodCount());
				orderdetail.setOrders(order);
				order.getOrderdetails().add(orderdetail);
			}
			orderService.add(order);
			//保存订单数据，用于页面回显
			model.addAttribute("order", order);
			//清空购物车
			cart.clearCart();
			return "reception/detail/clientOrderList";
		}else
			return "backstage/detail/orderList";
	}
	
	@RequestMapping("callPay")
	public void callPay(Orders order,HttpServletRequest request,HttpServletResponse response) throws IOException {
		// 1.修改数据:

		// 修改订单

		// 2.完成付款:
		// 付款需要的参数:
		String p0_Cmd = "Buy"; // 业务类型:
		String p1_MerId = "10001126856";// 商户编号:
		String p2_Order = "1006";// 订单编号:
		String p3_Amt = "0.01"; // 付款金额:
		String p4_Cur = "CNY"; // 交易币种:
		String p5_Pid = ""; // 商品名称:
		String p6_Pcat = ""; // 商品种类:
		String p7_Pdesc = ""; // 商品描述:
		String p8_Url = "http://192.168.1.136:8080/everyOrder/callBack.do"; // 商户接收支付成功数据的地址:
		String p9_SAF = ""; // 送货地址:
		String pa_MP = ""; // 商户扩展信息:
		String pd_FrpId = "CCB-NET-B2C";// 支付通道编码:
		String pr_NeedResponse = "1"; // 应答机制:
		String keyValue = "69cl522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv769220IuYe9u37N4y7rI4Pl"; // 秘钥
		String hmac = PaymentUtil.buildHmac(p0_Cmd, p1_MerId, p2_Order, p3_Amt,
				p4_Cur, p5_Pid, p6_Pcat, p7_Pdesc, p8_Url, p9_SAF, pa_MP,
				pd_FrpId, pr_NeedResponse, keyValue); // hmac
		// 向易宝发送请求:
		StringBuffer sb = new StringBuffer("https://www.yeepay.com/app-merchant-proxy/node?");
		sb.append("p0_Cmd=").append(p0_Cmd).append("&");
		sb.append("p1_MerId=").append(p1_MerId).append("&");
		sb.append("p2_Order=").append(p2_Order).append("&");
		sb.append("p3_Amt=").append(p3_Amt).append("&");
		sb.append("p4_Cur=").append(p4_Cur).append("&");
		sb.append("p5_Pid=").append(p5_Pid).append("&");
		sb.append("p6_Pcat=").append(p6_Pcat).append("&");
		sb.append("p7_Pdesc=").append(p7_Pdesc).append("&");
		sb.append("p8_Url=").append(p8_Url).append("&");
		sb.append("p9_SAF=").append(p9_SAF).append("&");
		sb.append("pa_MP=").append(pa_MP).append("&");
		sb.append("pd_FrpId=").append(pd_FrpId).append("&");
		sb.append("pr_NeedResponse=").append(pr_NeedResponse).append("&");
		sb.append("hmac=").append(hmac);
		// 重定向:向易宝出发:
		response.sendRedirect(sb.toString());
	}
	
	
	public OrderService getOrderService() {
		return orderService;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
}
